<template>
    <view>
        <!-- 枚举-单选 -->
        <template v-if="props.propValue.type === 'single_select'">
            <j-cell-group>
                <template v-for="(item, index) in props.propValue.valueNames">
                    <j-cell :key="index" :title="item" :value="index" :more="false" @jftap="onBindtapSelect(index)">
                        <view slot="tips">
							<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025" v-if="props.propValue.usedValue === index"></uni-icons>
                        </view>
                    </j-cell>
                </template>
            </j-cell-group>
        </template>

        <!-- 枚举-多选 -->
        <template v-if="props.propValue.type === 'multi_select'">
			<view>枚举-多选</view>
		</template>

        <!-- 范围 -->
        <template v-if="props.propValue.type === 'multi_scope'">
            <view style="padding: 20rpx">
                <view>
                    <view>最小值：</view>
                    <slider
                        show-value
                        :min="props.propValue.limitMinValue"
                        :max="props.propValue.limitMaxValue"
                        step="1"
                        :value="props.propValue.usedMinValue"
                        @change="onBindchangeMin"
                        @changing="onBindchangeingMin"
                    ></slider>
                </view>
                <view>
                    <view>最大值：</view>
                    <slider
                        show-value
                        :min="props.propValue.limitMinValue"
                        :max="props.propValue.limitMaxValue"
                        step="1"
                        :value="props.propValue.usedMaxValue"
                        @change="onBindchangeMax"
                        @changing="onBindchangeingMax"
                    ></slider>
                </view>
            </view>
        </template>

        <!-- 滑块 -->
        <template v-if="props.propValue.type === 'single_scope'">
            <view style="padding: 20rpx; font-size: 26rpx">百分比</view>
            <view style="padding: 20rpx; background: #ffffff">
                <slider
                    show-value
                    :min="props.propValue.limitMinValue"
                    :max="props.propValue.limitMaxValue"
                    step="1"
                    :value="props.propValue.usedValue"
                    @change="onBindchangeSlide"
                ></slider>
            </view>
        </template>

        <!-- 开关 -->
        <template v-if="props.propValue.type === 'switch'">
            <view style="padding: 20rpx; display: flex; justify-content: space-between; align-items: center; background: #ffffff">
                <view>设备开关</view>
                <switch :checked="props.propValue.usedValue === 1 || props.propValue.usedValue === 'on'" @change="onBindchangeOnOff"></switch>
            </view>
        </template>

        <!-- 确定按钮 -->
        <view class="bottom-fixed">
            <button type="primary" class="block radius-btn" @tap="onBindtapCreate">创建或修改条件</button>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            device: {},
            props: {
                propValue: {
                    type: '',
                    valueNames: [],
                    usedValue: '',
                    limitMinValue: '',
                    limitMaxValue: '',
                    usedMinValue: '',
                    usedMaxValue: ''
                }
            },
            enter: false
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        console.log('=== scene:condition-status-prop-control:onLoad:', options); 
		
		// 从create页面直接跳转进来
        if (options.condition) {
            let condition = JSON.parse(decodeURIComponent(options.condition));
            console.log('当前条件：', condition);
            this.device = condition.refDevice
            this.props = condition.conditionValue
            this.enter = true
        } 
		
		// 一步一步操作进来
        if (options.device && options.props) {
            let device = JSON.parse(decodeURIComponent(options.device));
            let props = JSON.parse(decodeURIComponent(options.props));
            console.log('当前设备：', device);
            console.log('当前属性：', props);
			this.device = device
			this.props = props
			this.enter = false
			
            // 读取缓存或赋值

            let conditionList = uni.getStorageSync('scene').conditionList; // getApp().globalData.scene.conditionList

            for (let i = 0; i < conditionList.length; i++) {
                const item = conditionList[i]; // 当前设备并且当前属性

                if (item.conditionRefId === device.deviceNo && (item.conditionValue.id === props.id || item.conditionValue.id === props.id)) {
                    this.props.propValue = item.conditionValue.propValue;
                }
            }
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log('=== scene:condition-status-prop-control:onShow:');
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        console.log('=== scene:condition-status-prop-control:onReady:');
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        console.log('--- scene:condition-status-prop-control:onHide:');
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        console.log('--- scene:condition-status-prop-control:onUnload:');
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        // 选择
        onBindtapSelect(ev) {
            console.log('选择属性：', ev);
            this.props.propValue.usedValue = ev;
        },

        // 开关
        onBindchangeOnOff(ev) {
            console.log('选择属性：', ev, this.props);

            if (ev.detail.value) {
                this.props.propValue.usedValue = 1;
            } else {
                this.props.propValue.usedValue = 0;
            }
        },

        // 滑块
        onBindchangeSlide(ev) {
            console.log('滑块', ev);
            this.props.propValue.usedValue = ev.detail.value;
        },

        // 最大值
        onBindchangeMax(ev) {
            console.log('最大值:', ev.detail.value);

            if (ev.detail.value <= this.props.propValue.usedMinValue) {
                this.props.propValue.usedMaxValue = this.props.propValue.usedMinValue;
            } else {
                this.props.propValue.usedMaxValue = ev.detail.value;
            }
        },

        onBindchangeingMax(ev) {
            console.log(ev.detail.value);
        },

        // 最小值
        onBindchangeMin(ev) {
            console.log('最小值:', ev.detail.value);

            if (ev.detail.value >= this.props.propValue.usedMaxValue) {
                this.props.propValue.usedMinValue = this.props.propValue.usedMaxValue;
            } else {
                this.props.propValue.usedMinValue = ev.detail.value;
            }
        },

        onBindchangeingMin(ev) {
            console.log(ev.detail.value);
        },

        onBindtapCreate() {
            // let scene = wx.getStorageSync('scene')
            let conditionList = uni.getStorageSync('scene').conditionList; // getApp().globalData.scene.conditionList

            let bool = false; // 判断是否有缓存
            // 更新

            for (let i = 0; i < conditionList.length; i++) {
                const item = conditionList[i];
                console.log('@@@@@@@@@@@@@', item); // 当前设备当前属性

                if (
                    item.conditionType === 0 &&
                    (item.conditionRefId === this.device.deviceNo || item.conditionRefId === this.device.id) &&
                    (item.conditionValue.id === this.props.id || item.conditionValue.id === this.props.id)
                ) {
                    bool = true;
                    item.conditionValue.propValue = this.props.propValue;
					item.conditionValue.propName = this.props.propName;
                    item.refDevice = this.device;
                    break;
                }
            } // 新建

            if (!bool) {
                let condition = {
                    conditionType: 0,
                    conditionRefId: this.device.deviceNo,
                    conditionName: this.device.deviceName,
                    conditionValue: {
                        id: this.props.id,
						propName:this.props.propName,
                        propValue: this.props.propValue
                    },
                    refDevice: this.device
                };
                conditionList.push(condition);
            } // scene.conditionList = conditionList
            // wx.setStorageSync('scene', scene)

            getApp().syncScene(this, {
                conditionList: conditionList
            });
            uni.navigateBack({
                delta: this.enter ? 1 : 3
            });
        }
    }
};
</script>
<style></style>
